package cn.bluethink.utile;

import cn.bluethink.graphical.Line;
import cn.bluethink.graphical.Polygon;
import cn.bluethink.graphical.Polyline;
import cn.bluethink.view.Graph;
import org.springframework.util.StringUtils;

import java.awt.*;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GraphicalUtil {

    /**封装多边形、折线中的节点 将x、y的坐标返回
     * @param points
     * @return
     */
    public static Map<String,int[]> getXAndY(List<Point> points){
        int[] xPoints = new int[points.size()];
        int[] yPoints = new int[points.size()];
        for (int i = 0; i < points.size(); i++) {
            xPoints[i]=points.get(i).x;
            yPoints[i]=points.get(i).y;
        }
        Map<String,int[]> res = new HashMap<>();
        res.put("x",xPoints);
        res.put("y",yPoints);
        return res;
    }

    /**
     * 保存画板上图形的相关信息 包括颜色 节点坐标等
     * @param path 请求路径save(新增) update(更新)
     * @param graphicalId 图形id
     * @param graph
     * @return
     * @throws IOException
     */
    public static String saveGraphInfo(String path, String graphicalId, Graph graph) throws IOException {

        if (AuthenticationUtil.isAuthentication()){

            int pointCount = graph.getHandleCount();

            StringBuffer nodes = new StringBuffer();
            StringBuffer start = new StringBuffer("[");
            StringBuffer end = new StringBuffer("]");

            for (int i = 0;i < pointCount;i++){
                if (i != pointCount - 1){
                    nodes = nodes.append("(").append(graph.getHandle(i).getX()).append(",").append(graph.getHandle(i).getY()).append(");");
                }else {
                    nodes = nodes.append("(").append(graph.getHandle(i).getX()).append(",").append(graph.getHandle(i).getY()).append(")");
                }
            }

            //要保存图形的节点坐标字符串
            StringBuffer nodesInfo = start.append(nodes).append(end);
            //要保存图形的类型
            String type = graph.type();
            //将图片颜色以String形式保存
            String colorObject = ColorUtil.getColor().toString();
            String color = colorObject.substring(15,colorObject.length() - 1);

            if (!StringUtils.isEmpty(path) && path.equals("save")){
                return HttpUtil.sendGet("http://localhost/graphical/" + path,"nodes=" + URLEncoder.encode(String.valueOf(nodesInfo),"utf-8") + "&type=" + type + "&color=" + color + "&userId=" + AuthenticationUtil.userId);
            }else{
                return HttpUtil.sendGet("http://localhost/graphical/" + path,"nodes=" + URLEncoder.encode(String.valueOf(nodesInfo),"utf-8") + "&id=" + graphicalId);
            }

        }

        return null;
    }

    /**
     * 解析字符串中点的坐标 并返回相应的类型折线 | 多边形
     * @param graphicalInfo
     * @param type
     * @return
     */
    public static Graph getGraphByType(String graphicalInfo,String type){
        List<Point> points = new ArrayList<>();
        //去中括号
        String index = graphicalInfo.substring(1,graphicalInfo.length() - 1);

        String[] coordinates = index.split(";");

        for (String coordinate : coordinates){
            String[] xAndy = coordinate.substring(1,coordinate.length() - 1).split(",");
            Point point = new Point();
            point.setLocation(Double.valueOf(xAndy[0]),Double.valueOf(xAndy[1]));
            points.add(point);
        }

        if (type.equals("polyline")){
            Polyline polyline = new Polyline();
            polyline.setPoints(points);
            return polyline;
        }else if (type.equals("polygon")){
            Polygon polygon = new Polygon();
            polygon.setPoints(points);
            return polygon;
        }else {
            Line line = new Line();
            line.setmFromPoint(points.get(0));
            line.setmToPoint(points.get(1));
            return line;
        }

    }
}
